Matcher m3 = p3.matcher("[che][1]'s blog is [rebey.cn][2],and built in [2016][3].");
while(m3.find()) {
System.out.println(m3.group());
}
Pattern p4 = Pattern.compile("(?>(\\[.+))");
Matcher m4 = p4.matcher("[che][1]'s blog is [rebey.cn][2],and built in [2016][3].");
while(m4.find()) {
System.out.println(m4.group());
}
结果皆为:[che][1]'s blog is [rebey.cn][2],and built in [2016][3].
注意括号。
说点什么
Possessive quantifiers are a way to prevent the regex engine from trying all permutations.
占有量词是一种用来组织正则表达式尝试所有排列组合的方式。(即不回溯)
With a possessive quantifier, the deal is all or nothing. 使用占有量词只有两种结果,全匹配或者空匹配。 The main practical benefit of possessive quantifiers is to speed up your regular expression. 占有量词的主要实际意义是加速你的正则表达式。